package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Persistence.MyVideosInfoEntity;
import com.echoesnet.echoesshortvideo.Repositories.MyVideosInfoRepository;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责驱动UI
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:05
 * @description
 */
public class ListOfMyVideoViewModel extends ViewModel
{

    private MyVideosInfoRepository tinyVideoInfoRepository;

    private MutableLiveData<Map<String, Object>> tinyVideoInfoList = new MutableLiveData<>();
    private MutableLiveData<Map<String, String>> checkVideoResult = new MutableLiveData<>();

    public LiveData<Map<String, Object>> getMyVideosList()
    {
        return tinyVideoInfoList;
    }

    public MutableLiveData<Map<String, String>> getCheckVideoResult()
    {
        return checkVideoResult;
    }

    public ListOfMyVideoViewModel()
    {
        this.tinyVideoInfoRepository = new MyVideosInfoRepository();
    }


    public void requestMyVideosList(int start, int step, boolean isRefresh)
    {
        tinyVideoInfoRepository.getMyVideosList(start, step, new SilenceSubscriber<String>()
        {

            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    String total = jsonObject.getString("total");
                    String rows = jsonObject.getString("rows");
                    List<MyVideosInfoEntity> list = new Gson().fromJson(rows, new TypeToken<List<MyVideosInfoEntity>>()
                    {
                    }.getType());

                    Map<String, Object> map = new HashMap<>();
                    map.put("isRefresh", isRefresh);
                    map.put("totalNum", total);
                    map.put("videoList", list);

                    tinyVideoInfoList.setValue(map);

                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

    }

    public void requestVideosList(String uid, int start, int step, boolean isRefresh)
    {
        tinyVideoInfoRepository.getVideosList(uid, start, step, new SilenceSubscriber<String>()
        {

            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject jsonObject = new JSONObject(response);
                    String total = jsonObject.getString("total");
                    String rows = jsonObject.getString("rows");
                    List<MyVideosInfoEntity> list = new Gson().fromJson(rows, new TypeToken<List<MyVideosInfoEntity>>()
                    {
                    }.getType());

                    Map<String, Object> map = new HashMap<>();
                    map.put("isRefresh", isRefresh);
                    map.put("totalNum", total);
                    map.put("videoList", list);

                    tinyVideoInfoList.setValue(map);

                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

    }

    public void checkVideoState(String videoId, String toUserId, String openSource)
    {
        tinyVideoInfoRepository.checkVideo(videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject object = new JSONObject(response);
                    String result = object.getString("delStatus");
                    Map<String, String> map = new HashMap();
                    map.put("videoId", videoId);
                    map.put("toUserId", toUserId);
                    map.put("openSource", openSource);
                    map.put("result", result);
                    checkVideoResult.setValue(map);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}
